<template>
  <div>
    <van-pull-refresh v-model="isRefresh" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <art-item
          v-for="item in artlist"
          :key="item.art_id.toString()"
          :article="item"
        ></art-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { getArtListAPI } from '@/api/homeAPI.js'
import ArtItem from '@/components/ArtItem/ArtItem.vue'
export default {
  name: 'ArtList',
  data() {
    return {
      // 放文章列表数据的
      artlist: [],
      // 是否正在进行上拉加载的请求
      loading: false,
      // 所有数据是否加载完毕了
      finished: false,
      // 是否正在进行下拉刷新的数据请求
      isRefresh: false
    }
  },
  props: {
    channelId: {
      type: Number,
      required: true
    }
  },
  created() {
    this.initArtList()
  },

  components: {
    ArtItem
  },

  computed: {},

  mounted() {},

  methods: {
    async initArtList(state) {
      const { data: res } = await getArtListAPI(this.channelId)
      console.log(res)
      if (res.message === 'OK') {
        // this.artlist = res.data.results
        if (state) {
          console.log('下拉刷新的数据合并')
          this.artlist = [...res.data.results, ...this.artlist]
          this.isRefresh = false
        } else {
          console.log('上拉加载数据的合并')
          this.artlist = [...this.artlist, ...res.data.results]
          this.loading = false
        }

        if (res.data.results.length === 0) {
          this.finished = true
        }
      }
    },
    onLoad() {
      // 触发上拉加载更多数据
      console.log('触发上拉加载onload')
      this.initArtList()
    },
    onRefresh() {
      console.log('触发下拉刷新')
      this.initArtList(true)
    }
  }
}
</script>
<style lang="less" scoped></style>
